<?php 
	class Privileges extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

    	/**
		* 函数功能介绍：插入菜单表数据
		* 创建人: Zhou Hailong
		* 创建时间: 2014-08-20 15:11
		* 最后修改人：
		* 最后修改时间：
		* @param data array
		* @return boolen
		*/
		function insert($data){
       		$result = $this->db->insert('sys_privileges', $data);
       		return $result;
		}

		/**
		* 函数功能介绍：根据PID返回菜单列表
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-20 16:04
		* 最后修改人：
		* 最后修改时间：
		* @param pid int
		* @return array
		*/
		function get_list($status = FALSE, $web_id = 'all'){
			$where = '1 = 1';

			if($web_id > 0 || $web_id === 0){
				$where .= " AND web_id = $web_id";
			}

			if($status !== FALSE){
				$where .= " AND status = $status";
			}

			$sql = "SELECT id, pid, action, menu_title, menu_url, sort, add_time, status FROM {$this->db->dbprefix}sys_privileges WHERE $where ORDER BY sort ASC";
			$result = $this->db->query($sql)->result();
			
			$menu_list = array();
			foreach ($result as $key => $value) {
				if($value->pid == 0){
					$menu_list[] = $value;
					unset($result[$key]);
				}
			}

			foreach ($menu_list as $key => $value) {
				$menu_list[$key]->sub = array();
				foreach ($result as $k => $val) {
					if($val->pid == $value->id){
						$menu_list[$key]->sub[] = $val;
					}
				}
			}
       		return $menu_list;
		}

		/**
		* 函数功能介绍：根据ID返回菜单详细信息
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-20 16:04
		* 最后修改人：
		* 最后修改时间：
		* @param id int
		* @return array
		*/
		function get_privilege_info($id){
			$sql = "SELECT * FROM {$this->db->dbprefix}sys_privileges WHERE id=$id";
			$result = $this->db->query($sql)->result();
			return $result;
		}

		/**
		* 函数功能介绍：执行删除权限菜单数据的操作
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-21 10:08
		* 最后修改人：
		* 最后修改时间：
		* @param id int
		* @return array
		*/
		function delete($id){
			$this->db->where('id', $id);
			$result = $this->db->delete('sys_privileges');
			return $result;
		}

		/**
		* 函数功能介绍：返回权限菜单列表（用于分页显示）
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-21 11:08
		* 最后修改人：
		* 最后修改时间：
		* @param id int
		* @return array
		*/
		function get_list_ofpage($limit = 0, $offset = 10, $conditions = array()){
			$where = "1=1";
			$order = 'pid ASC';
			if(count($conditions) > 0){
				if(isset($conditions['sort']) && $conditions['asc_des'] > 0){
					$sort_order = array(1 => ' DESC', 2 => ' ASC');
					switch ($conditions['sort']) {
						case 1:
							$order = 'action';
							break;
						case 2:
							$order = 'menu_url';
							break;
						case 3:
							$order = 'add_time';
							break;
						case 4:
							$order = 'status';
							break;
						case 5:
							$order = 'sort';
							break;
					}
					$order .= $sort_order[$conditions['asc_des']];
				}
				if(isset($conditions['pid']) && $conditions['pid'] >= 0 && $conditions['pid'] != 'all'){
					$where .= ' AND pid = ' . $conditions['pid'];
				}

				if(isset($conditions['search']) && $conditions['search']){
					$where .= ' AND menu_title like "%'.$conditions['search'].'%"';
				}
			}
			$sql = "SELECT id, pid, action, menu_title, menu_url, sort, add_time, status FROM {$this->db->dbprefix}sys_privileges WHERE $where ORDER BY $order LIMIT $limit, $offset";
			$result = $this->db->query($sql)->result();
			$sql = "SELECT count(id) AS count FROM {$this->db->dbprefix}sys_privileges WHERE $where";
			$count = $this->db->query($sql)->result();
       		return array("count" => $count[0]->count, "result" => $result);
		}

		/**
		* 函数功能介绍：根据指定的查询条件返回指定的权限字段
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-22 11:51
		* 最后修改人：
		* 最后修改时间：
		* @param id int
		* @return array
		*/
		function get_field_by_where($fields, $where = "1=1"){
			$sql = "SELECT $fields FROM {$this->db->dbprefix}sys_privileges WHERE $where";
			$result = $this->db->query($sql)->row_array();
			return $result;
		}

		function batch_delete($ids){
			$ids = implode(",",$ids);
			$sql = "DELETE FROM {$this->db->dbprefix}sys_privileges WHERE id in($ids)";
			$this->db->query($sql);
		}

		function update_privilege_sort($id, $data){
			$this->db->where('id', $id);
 			return $this->db->update('sys_privileges', $data);
		}
	}